﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.MarketData
{
    public class MarketDates
    {
        /*
        /// <summary>
        /// MS generated sql class to access liquid view database
        /// </summary>
        Algobox.JuicyLib.SQL.LiquidView.LiquidViewLinqDataContext liquidViewLinq = null;


        public MarketDates()
        {
            liquidViewLinq = new Algobox.JuicyLib.SQL.LiquidView.LiquidViewLinqDataContext();
        }
        */

        Dictionary<string, DateTime> dateCache = new Dictionary<string, DateTime>();

        Algobox.JuicyLib.SQL.LiquidView.LiquidViewLinqDataContext liquidViewLinq = new Algobox.JuicyLib.SQL.LiquidView.LiquidViewLinqDataContext();

        public DateTime GetLastTradingDate(string hostType, string exchangeIdentifier, DateTime fromDate)
        {
            string key = hostType + "-" + exchangeIdentifier + "-" + fromDate.ToShortDateString();

            if (!dateCache.ContainsKey(key))
            {                
                DateTime? lastDate = fromDate;
                liquidViewLinq.GetLastTradingDate(hostType, exchangeIdentifier, ref lastDate);

                // add date to cache
                dateCache.Add(key, lastDate.Value);
                return lastDate.Value;
            }
            else
            {
                return dateCache[key];
            }
        }

    }
}
